/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.miit.elearning.services.curriculum;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import org.dozer.DozerBeanMapperSingletonWrapper;
import org.hibernate.Session;
import ru.miit.elearning.hibernate.dao.ConfigurationDAO;
import ru.miit.elearning.hibernate.dao.CourseDAO;
import ru.miit.elearning.hibernate.dao.CurriculumDAO;
import ru.miit.elearning.hibernate.dao.HibernateUtil;
import ru.miit.elearning.hibernate.dto.CurriculumLevel;
import ru.miit.elearning.hibernate.dto.UserIdentity;

/**
 *
 * @author Cyclone
 */
@WebService()
public class Curriculum {

    /**
     * Web service operation
     */
    @WebMethod(operationName = "getCurriculumDescription")
    public String getCurriculumDescription(@WebParam(name = "curriculumId") int curriculumId) {
        Session session = null;
        String description = null;

        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            session.beginTransaction();
            description = (new CurriculumDAO(session)).findCurriculumDescription(curriculumId);

            if (session.isOpen()) {
                session.getTransaction().commit();
            }

        } catch (Exception e) {
            Logger.getLogger(Curriculum.class.getName()).log(Level.SEVERE, null, e);
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }

        return description;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "getStudentsByCourse")
    public java.util.List<ru.miit.elearning.services.curriculum.VlStudent> getStudentsByCourse(@WebParam(name = "courseId") int courseId) {

        Session session = null;
        List<VlStudent> studentList = null;
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            session.beginTransaction();

            List<UserIdentity> userList = new CourseDAO(session).findStudentsByCourse(courseId);
            if (userList != null) {
                studentList = new ArrayList<VlStudent>(userList.size());
                for (UserIdentity ui : userList) {
                    studentList.add(DozerBeanMapperSingletonWrapper.getInstance().map(ui, VlStudent.class));
                }
            }


            if (session != null && session.isOpen()) {
                session.getTransaction().commit();
            }
        } catch (Exception e) {
            Logger.getLogger(Curriculum.class.getName()).log(Level.SEVERE, null, e);
            e.printStackTrace();

            if (session != null && session.isOpen()) {
                session.getTransaction().rollback();
            }
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return studentList;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "getLabDescription")
    public String getLabDescription(@WebParam(name = "courseId") int courseId, @WebParam(name = "labNumber") int labNumber) {
        Session session = null;
        String description = null;

        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            session.beginTransaction();
            description = (new CurriculumDAO(session)).findLabDescription(courseId, labNumber);

            if (session.isOpen()) {
                session.getTransaction().commit();
            }

        } catch (Exception e) {
            Logger.getLogger(Curriculum.class.getName()).log(Level.SEVERE, null, e);
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }

        return description;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "getLabsByCourse")
    public java.util.List<ru.miit.elearning.services.curriculum.VlLab> getLabsByCourse(@WebParam(name = "courseId") int courseId) {
        Session session = null;
        List<VlLab> vlLabList = null;
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            session.beginTransaction();

            List<CurriculumLevel> labList = new ArrayList<CurriculumLevel>(new CourseDAO(session).findCourse(courseId).getLabList());

            if (labList != null) {
                vlLabList = new ArrayList<VlLab>(labList.size());
                for (CurriculumLevel lab : labList) {
                    vlLabList.add(DozerBeanMapperSingletonWrapper.getInstance().map(lab, VlLab.class));
                }
            }


            if (session != null && session.isOpen()) {
                session.getTransaction().commit();
            }
        } catch (Exception e) {
            Logger.getLogger(Curriculum.class.getName()).log(Level.SEVERE, null, e);
            e.printStackTrace();

            if (session != null && session.isOpen()) {
                session.getTransaction().rollback();
            }
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return vlLabList;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "getUserPropertyValueByAlias")
    public String getUserPropertyValueByAlias(@WebParam(name = "alias") String alias, @WebParam(name = "userId") int userId) {
        Session session = null;
        String value = null;
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            session.beginTransaction();

            ConfigurationDAO configurationDAO = new ConfigurationDAO(session);
            value = configurationDAO.findUserPropertyValueByAlias(userId, alias);

            if (session != null && session.isOpen()) {
                session.getTransaction().commit();
            }
        } catch (Exception e) {
            Logger.getLogger(Curriculum.class.getName()).log(Level.SEVERE, null, e);
            e.printStackTrace();

            if (session != null && session.isOpen()) {
                session.getTransaction().rollback();
            }
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return value;
    }

    /**
     * Web service operation
     * @return true for cussessfull lab approvement; false for failure
     */
    @WebMethod(operationName = "approveLab")
    public boolean approveLab(@WebParam(name = "studentId") int studentId, @WebParam(name = "courseId") int courseId, @WebParam(name = "labNumber") int labNumber, @WebParam(name = "tutorLogin") String tutorLogin, @WebParam(name = "approve") boolean approve) {
        Session session = null;
        boolean completed = false;

        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            session.beginTransaction();

            CurriculumDAO curriculumDAO = new CurriculumDAO(session);
            completed = curriculumDAO.approveLab(studentId, courseId, labNumber, curriculumDAO.findUserIdByLogin(tutorLogin) , approve);

            if (session != null && session.isOpen()) {
                session.getTransaction().commit();
            }
        } catch (Exception e) {
            if (session != null && session.isOpen()) {
                session.getTransaction().rollback();
            }
            Logger.getLogger(CurriculumDAO.class.getName()).log(Level.SEVERE, null, e);
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return completed;
    }
}
